#!/bin/sh
#
#   http://code.google.com/media-translate/
#   Copyright (C) 2010  Serge A. Timchenko
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#

#
# Translate CGI module
# 'rutube.ru' plug-in
#
# version: 1.0 13.08.2010 16:41
#
# http://rutube.ru/tv/o2tv.html
# http://rutube.ru/tracks/1088305.html?v=1c21315933044e39fdd57808ac5e3958
#

stream_type='video/x-flv'
stream_url=''
server_type='rutube'

local video_id=

if echo "${arg_url}" | grep -q -s "\(www\.\)*rutube\.ru.*[&?]v=[0-9a-f]*$"; then 
  video_id=`echo "$arg_url" | sed -e 's/.*[?&]v=//;s/&.*//`
  host_response=`$MSDL -q -o ${TMPFILE} -p http --useragent "${USERAGENT}" http://bl.rutube.ru/${video_id}.xml 2>&1`
  if [ -f ${TMPFILE} ]; then
    stream_url=`get_xml_text "finalAddress" | sed -e 's/<!\[CDATA\[//;s/\]\]>//'`
    protocol=`echo "$stream_url" | sed -e 's/:\/\/.*$//`
    rm -f $TMPFILE
    return $RC_OK
  fi
elif echo "${arg_url}" | grep -q -s "\(www\.\)*rutube\.ru/tv/.*$"; then 
  host_response=`$MSDL -q -o ${TMPFILE} -p http --useragent "${USERAGENT}" $arg_url 2>&1`
  if [ -f ${TMPFILE} ]; then
    video_id=`grep "playMovie('play'" ${TMPFILE} | sed "s/.*\/online\///;s/\.flv?.*//"`
    stream_url="http://webcaster.rutube.ru:8000/${video_id}?format=flv"
    protocol=`echo "$stream_url" | sed -e 's/:\/\/.*$//`
    icy_name=`grep 'meta name="description"' ${TMPFILE} | sed 's/.*t="//;s/".*//' | $XCODE -s +k | $TOUTF8`
    rm -f $TMPFILE
    return $RC_OK
  fi
fi

return $RC_FAIL
